/*
 * Race.cpp
 *
 *  Created on: Dec 31, 2008
 *      Author: sez
 */

#include <Xml/Xml.h>
#include <Xml/XmlPath.h>

#include "Race.h"
#include "Power.h"
#include "Trait.h"

Race::Race(const QString &newTitle)
    : TitledData(newTitle)
{
}

Race::Race(const Race &rhs)
    : TitledData(rhs.title())
{
    this->heightMin = rhs.heightMin;
    this->heightMax = rhs.heightMax;
    this->weightMin = rhs.weightMin;
    this->weightMax = rhs.weightMax;
    this->abilityBonus = rhs.abilityBonus;
    this->size = rhs.size;
    this->speed = rhs.speed;
    this->vision = rhs.vision;
    this->language = rhs.language;
    this->skillBonus = rhs.skillBonus;
    this->traits = rhs.traits;
    this->powers = rhs.powers;
    this->raceSummary = rhs.raceSummary;
}

Race::~Race()
{
}

void Race::saveToXml() const
{
    Xml xml(XmlPath(XmlPath::RACIAL_BASE, XmlPath::safeDataPathName(title())));
    xml.appendTag("Title", title());
    appendTag(xml, "HeightMin", heightMin);
    appendTag(xml, "HeightMax", heightMax);
    appendTag(xml, "WeightMin", weightMin);
    appendTag(xml, "WeightMax", weightMax);
    appendArray(xml, "AbilityBonus", abilityBonus);
    appendTag(xml, "Size", size);
    appendTag(xml, "Speed", speed);
    appendTag(xml, "Vision", vision);
    xml.appendTag("Summary", raceSummary);
    xml.appendTag("Description", raceDescription);
    appendArray(xml, "Language", language);
    appendArray(xml, "SkillBonus", skillBonus);
    appendTitleArray(xml, "Traits", traits);
    appendTitleArray(xml, "Powers", powers);
    xml.serializeXml();
}

void Race::loadFromXml()
{
    Xml xml(XmlPath(XmlPath::RACIAL_BASE, XmlPath::safeDataPathName(title())));
    xml.deserializeXml();
    setTitle(xml.value("Title"));
    heightMin = value<int>(xml, "HeightMin");
    heightMax = value<int>(xml, "HeightMax");
    weightMin = value<int>(xml, "WeightMin");
    weightMax = value<int>(xml, "WeightMax");
    abilityBonus = valueArray<DndData::Ability>(xml, "AbilityBonus");
    size = value<DndData::BodySize>(xml, "Size");
    speed = value<int>(xml, "Speed");
    vision = value<DndData::VisionType>(xml, "Vision");
    raceSummary = xml.value("Summary");
    raceDescription = xml.value("Description");
    language = valueArray<DndData::Language>(xml, "Language");
    skillBonus = valueArray<DndData::SkillTypes>(xml, "SkillBonus");
    traits = valueTitleArray<Trait>(xml, "Traits");
    powers = valueTitleArray<Power>(xml, "Powers");
}

Race &Race::operator=(const Race &rhs)
{
    this->heightMin = rhs.heightMin;
    this->heightMax = rhs.heightMax;
    this->weightMin = rhs.weightMin;
    this->weightMax = rhs.weightMax;
    this->abilityBonus = rhs.abilityBonus;
    this->size = rhs.size;
    this->speed = rhs.speed;
    this->vision = rhs.vision;
    this->language = rhs.language;
    this->skillBonus = rhs.skillBonus;
    this->traits = rhs.traits;
    this->powers = rhs.powers;
    this->raceSummary = rhs.raceSummary;
    return *this;
}
